import datePicker from './date-picker'
export default class DateTime {
	
	init(options) {
		let nowDate = new Date()
		let defaultOptions = Object.assign({
			start: "1900/01/01 00:00:00",
			end: "2100/12/31 00:00:00",
			current: nowDate.getFullYear() + "/" + (nowDate.getMonth() + 1) + "/" + nowDate.getDate() + " " + nowDate.getHours() + ":" + nowDate.getMinutes() + ":" + nowDate.getSeconds()
		}, options)

		this.startDateTime = new Date(defaultOptions.start)
		this.endDateTime = new Date(defaultOptions.end)
		this.currentDateTime = new Date(defaultOptions.current)
		this.dates = datePicker.getDate({
			start: defaultOptions.start,
			end: defaultOptions.end
		})
	}

	initYearRangeAndIndex() {
		let yearList = []
		let year = this.currentDateTime.getFullYear()
		let yearIndex = 0
		this.dates.forEach((item, index) => {
			yearList[index] = {
				label: item.label,
				value: item.value
			}
			if(item.value == year) {
				yearIndex = index
			}
		})
		this.yearRangeAndIndex = {
			range: yearList,
			index: yearIndex
		}
	}

	initMonthRangeAndIndex() {
		if(!this.yearRangeAndIndex) {
			this.initYearRangeAndIndex()
		}
		let monthList = []
		let month = this.currentDateTime.getMonth() + 1
		let yearIndex = this.yearRangeAndIndex.index
		let monthIndex = 0

		this.dates[yearIndex].children.forEach((item, index) => {
			monthList[index] = {
				label: item.label,
				value: item.value
			}
			if(item.value == month) {
				monthIndex = index
			}
		})

		this.monthRangeAndIndex = {
			range: monthList,
			index: monthIndex
		}
	}

	initDayRangeAndIndex() {
		if(!this.yearRangeAndIndex) {
			this.initYearRangeAndIndex()
		}
		if(!this.monthRangeAndIndex) {
			this.initMonthRangeAndIndex()
		}

		let dayList = []
		let day = this.currentDateTime.getDate()
		let yearIndex = this.yearRangeAndIndex.index
		let monthIndex = this.monthRangeAndIndex.index
		let dayIndex = 0

		this.dates[yearIndex].children[monthIndex].children.forEach((item, index) => {
			dayList[index] = {
				label: item.label,
				value: item.value
			}
			if(item.value == day) {
				dayIndex = index
			}
		})

		this.dayRangeAndIndex = {
			range: dayList,
			index: dayIndex
		}
	}

	ensureStartAndEnd(start, end, isHour = false) {
		let currentDateStamp = new Date(this.currentDateTime.getFullYear() + "/" + (this.currentDateTime.getMonth() + 1) + "/" + this.currentDateTime.getDate()).getTime()
		let startDateStamp = new Date(this.startDateTime.getFullYear() + "/" + (this.startDateTime.getMonth() + 1) + "/" + this.startDateTime.getDate()).getTime()// + " " + this.startDateTime.getHours() + ":" + this.startDateTime.getMinutes() + ":" + this.startDateTime.getSeconds()
		let endDateStamp = new Date(this.endDateTime.getFullYear() + "/" + (this.endDateTime.getMonth() + 1) + "/" + this.endDateTime.getDate()).getTime()// + " " + this.endDateTime.getHours() + ":" + this.endDateTime.getMinutes() + ":" + this.endDateTime.getSeconds()
		
		let startIndex = 0
		let endIndex = 0
		if(currentDateStamp - startDateStamp == 0) {
			if(endDateStamp - currentDateStamp == 0) {
				startIndex = start
				endIndex = end
			} else {
				///
				startIndex = start
				endIndex = isHour ? 23:59
			}
		} else {
			if(endDateStamp - currentDateStamp == 0) {
				startIndex = 0
				endIndex = end
			} else {
				startIndex = 0
				endIndex = isHour ? 23:59
			}
		}

		if(new Date(2100/12/31).getTime() - currentDateStamp == 0) {
			startIndex = 0
			endIndex = 0
		}

		return {
			startIndex: startIndex,
			endIndex: endIndex
		}
	}

	initHourRangeAndIndex() {
		let {startIndex, endIndex} = this.ensureStartAndEnd(this.startDateTime.getHours(), this.endDateTime.getHours(), true)

		let hour = this.currentDateTime.getHours()
		let hourList = []
		let hourIndex = 0
		for(let value=startIndex, index = 0; value<=endIndex; value++, index++) {
			hourList[index] = {
				label: this.format(value) + "时",
				value: value
			}
			if(hour == value) {
				hourIndex = index
			}
		}

		this.hourRangeAndIndex = {
			range: hourList,
			index: hourIndex
		}
	}

	initMinuteRangeAndIndex() {
		let {startIndex, endIndex} = this.ensureStartAndEnd(this.startDateTime.getMinutes(), this.endDateTime.getMinutes())
		let minute = this.currentDateTime.getMinutes()

		let currentHours = this.currentDateTime.getHours()
		let startHours = this.startDateTime.getHours()
		if(currentHours - startHours > 0) {
			startIndex = 0
		}

		let minuteList = []
		let minuteIndex = 0
		for(let value=startIndex, index = 0; value<=endIndex; value++, index++) {
			minuteList[index] = {
				label: this.format(value) + "分",
				value: value
			}
			if(minute == value) {
				minuteIndex = index
			}
		}

		this.minuteRangeAndIndex = {
			range: minuteList,
			index: minuteIndex
		}
	}

	initSecondRangeAndIndex() {
		let {startIndex, endIndex} = this.ensureStartAndEnd(this.startDateTime.getSeconds(), this.endDateTime.getSeconds())
		let second = this.currentDateTime.getSeconds()
		
		let currentMinutes = this.currentDateTime.getMinutes()
		let startMinutes = this.startDateTime.getMinutes()
		
		let currentHours = this.currentDateTime.getHours()
		let startHours = this.startDateTime.getHours()
		
		if(currentMinutes - startMinutes > 0 || currentHours - startHours > 0) {
			startIndex = 0
		}
		
		let secondList = []
		let secondIndex = 0
		for(let value=startIndex, index = 0; value<=endIndex; value++, index++) {
			secondList[index] = {
				label: this.format(value) + "秒",
				value: value
			}
			if(second == value) {
				secondIndex = index
			}
		}

		this.secondRangeAndIndex = {
			range: secondList,
			index: secondIndex
		}
	}

	getCurrent(indexArr = [], level = 5) {
		let year = 1900
		let month = 1
		let day = 1
		let hour = 0
		let minute = 0
		let second = 0

		if(this.yearRangeAndIndex && indexArr.length > 0) {
			year = this.yearRangeAndIndex.range[indexArr[0]].value
		}
		if(this.monthRangeAndIndex && indexArr.length > 1) {
			month = this.monthRangeAndIndex.range[indexArr[1]].value
		}
		if(this.dayRangeAndIndex && indexArr.length > 2) {
			day = this.dayRangeAndIndex.range[indexArr[2]].value
		}
		if(this.hourRangeAndIndex && indexArr.length > 3) {
			hour = this.hourRangeAndIndex.range[indexArr[3]].value
		}
		if(this.minuteRangeAndIndex && indexArr.length > 4) {
			minute = this.minuteRangeAndIndex.range[indexArr[4]].value
		}
		if(this.secondRangeAndIndex && indexArr.length > 5) {
			second = this.secondRangeAndIndex.range[indexArr[5]].value
		}

		if(month == 2) {
			if(year % 4 == 0) {
				//闰年,29天
				day = Math.min(day, 29)
			} else {
				//平年,28天
				day = Math.min(day, 28)
			}
		} else {
			let arr = [1, 3, 5, 7, 8, 10, 12]
			if(arr.indexOf(month) == -1) {
				if(day > 30) {
					day = 30
				}
			}
		}
		if(level == 1) {
			return (year + "/" + this.format(month))
		} else if(level == 2) {
			return (year + "/" + this.format(month))
		} else if(level == 3) {
			return (year + "/" + this.format(month) + "/" + this.format(day))
		} else if(level == 4) {
			return (year + "/" + this.format(month) + "/" + this.format(day) + " " + this.format(hour))
		} else if(level == 5) {
			return (year + "/" + this.format(month) + "/" + this.format(day) + " " + this.format(hour) + ":" + this.format(minute))
		} else if(level == 6) {
			return (year + "/" + this.format(month) + "/" + this.format(day) + " " + this.format(hour) + ":" + this.format(minute) + ":" + this.format(second))
		} else {
			return (year + "/" + this.format(month) + "/" + this.format(day) + " " + this.format(hour) + ":" + this.format(minute) + ":" + this.format(second))
		}
	}

	format(value) {
		if(Number(value) < 10) {
			return "0" + value
		}
		return "" + value
	}
}